// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import Vuex from 'vuex'
import 'element-ui/lib/theme-chalk/index.css'
import {getRequest} from "./util/api";
import {postRequest} from "./util/api";
import {putRequest} from "./util/api";
import {deleteRequest} from "./util/api";
import {postKeyValueRequest} from "./util/api";
import store from './store/index'
import {initMenu} from "./util/menus";
import treeTransfer from "el-tree-transfer";
Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.use(Vuex)
Vue.use(treeTransfer)
Vue.prototype.getRequest = getRequest;
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.postKeyValueRequest = postKeyValueRequest;


router.beforeEach((to, from, next) => {
  if (window.sessionStorage.getItem("user")) {
    if (to.path == '/') {
      next('/home');
    } else {
      initMenu(router, store);
      if (to.matched.length === 0) {
        next('/home')
      }
      else {
        next();
      }
    }
  } else {
    if (to.path == '/') {
      next();
    }
    else {
      next('/?redirect=' + to.path);
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
